package com.example.understand_compose1.testcase_activity

import android.app.Activity
import android.os.Bundle
import android.widget.Adapter
import android.widget.EditText
import android.widget.ImageButton
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.understand_compose1.R
import com.example.understand_compose1.testcase_data.adapter.MessagesAdapter
import com.example.understand_compose1.testcase_data.model.MessageData

// BUG: Bug page, maybe deprecate
class FetWechatActivity: Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.wechat_like_layout)
        val rv_messages: RecyclerView = findViewById(R.id.recyclerViewMessages)
        val et_message: EditText = findViewById(R.id.editTextMessage)
        val ib_send: ImageButton = findViewById(R.id.imageButtonSend)

        val messages = ArrayList<MessageData>()
        val adapter: MessagesAdapter = MessagesAdapter(messages)

        rv_messages.adapter = adapter
        rv_messages.layoutManager = LinearLayoutManager(this)

        ib_send.setOnClickListener {
            val msgText: String = et_message.text.toString()

            if (!msgText.isNotEmpty()) {
                val msgBuilder = MessageData(msgText, true)
                messages.add(msgBuilder)
                adapter.notifyItemInserted(messages.size - 1)

                rv_messages.scrollToPosition(messages.size - 1)
                et_message.setText("")
            }
        }
    }
}